package com.muyu.system.service.impl;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.system.domain.DanmoShoppingProducts;
import com.muyu.system.domain.DanmoShoppingProductsCollection;
import com.muyu.system.domain.DmShoppingGoods;
import com.muyu.system.mapper.DanmoShoppingProductsCollectionMapper;
import com.muyu.system.mapper.DmShoppingGoodsMapper;
import com.muyu.system.service.IDanmoShoppingProductsCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品收藏Service业务层处理
 *
 * @author yangle
 * @date 2024-10-09
 */
@Service
public class DanmoShoppingProductsCollectionServiceImpl implements IDanmoShoppingProductsCollectionService
{
    @Autowired
    private DanmoShoppingProductsCollectionMapper danmoShoppingProductsCollectionMapper;

    /**
     * 查询商品收藏
     *
     * @param id 商品收藏主键
     * @return 商品收藏
     */
    @Override
    public DanmoShoppingProductsCollection selectDanmoShoppingProductsCollectionById(Long id)
    {
        return danmoShoppingProductsCollectionMapper.selectDanmoShoppingProductsCollectionById(id);
    }

    /**
     * 查询商品收藏列表
     *
     * @param danmoShoppingProductsCollection 商品收藏
     * @return 商品收藏
     */
    @Override
    public List<DanmoShoppingProductsCollection> selectDanmoShoppingProductsCollectionList(DanmoShoppingProductsCollection danmoShoppingProductsCollection)
    {
        return danmoShoppingProductsCollectionMapper.selectDanmoShoppingProductsCollectionList(danmoShoppingProductsCollection);
    }

    /**
     * 新增商品收藏
     *
     * @param danmoShoppingProductsCollection 商品收藏
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDanmoShoppingProductsCollection(DanmoShoppingProductsCollection danmoShoppingProductsCollection)
    {
        danmoShoppingProductsCollection.setCreateTime(DateUtils.getNowDate());
        int rows = danmoShoppingProductsCollectionMapper.insertDanmoShoppingProductsCollection(danmoShoppingProductsCollection);
        insertDanmoShoppingProducts(danmoShoppingProductsCollection);
        return rows;
    }

    /**
     * 修改商品收藏
     *
     * @param danmoShoppingProductsCollection 商品收藏
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDanmoShoppingProductsCollection(DanmoShoppingProductsCollection danmoShoppingProductsCollection)
    {
        danmoShoppingProductsCollection.setUpdateTime(DateUtils.getNowDate());
        danmoShoppingProductsCollectionMapper.deleteDanmoShoppingProductsById(danmoShoppingProductsCollection.getId());
        insertDanmoShoppingProducts(danmoShoppingProductsCollection);
        return danmoShoppingProductsCollectionMapper.updateDanmoShoppingProductsCollection(danmoShoppingProductsCollection);
    }

    /**
     * 批量删除商品收藏
     *
     * @param ids 需要删除的商品收藏主键
     * @return 结果
     */
    @Transactional
    @Override
    public Result<Integer> deleteDanmoShoppingProductsCollectionByIds(Long[] ids)
    {
        //danmoShoppingProductsCollectionMapper.deleteDanmoShoppingProductsByIds(ids);
        int i = danmoShoppingProductsCollectionMapper.deleteDanmoShoppingProductsCollectionByIds(ids);
        return Result.success(i, "删除成功");

    }

    /**
     * 删除商品收藏信息
     *
     * @param id 商品收藏主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDanmoShoppingProductsCollectionById(Long id)
    {

        danmoShoppingProductsCollectionMapper.deleteDanmoShoppingProductsById(id);
        return danmoShoppingProductsCollectionMapper.deleteDanmoShoppingProductsCollectionById(id);
    }

    @Override
    public DanmoShoppingProductsCollection selectDanmoShoppingProductsCollectionLists(DanmoShoppingProductsCollection danmoShoppingProductsCollection) {
        return danmoShoppingProductsCollectionMapper.selectDanmoShoppingProductsCollectionLists(danmoShoppingProductsCollection);

    }


    @Override
    public void addCollection(DanmoShoppingProductsCollection danmoShoppingProductsCollection) {
         danmoShoppingProductsCollection.setCreateTime(DateUtils.getNowDate());
          danmoShoppingProductsCollectionMapper.addCollection(danmoShoppingProductsCollection);
    }

    @Autowired
    private DmShoppingGoodsMapper dmShoppingGoodsMapper;
    @Override
    public List<DanmoShoppingProductsCollection> selectDmGoodsLists(DanmoShoppingProductsCollection danmoShoppingProductsCollection) {
            // 查询所有商品的收藏数据
            List<DanmoShoppingProductsCollection> danmoShoppingProductsCollections = danmoShoppingProductsCollectionMapper.selectDanmoShoppingProductsCollectionList(danmoShoppingProductsCollection);

            // 提取所有商品 ID
            List<Long> goodsIds = danmoShoppingProductsCollections.stream()
                    .map(DanmoShoppingProductsCollection::getGoodsId)
                    .collect(Collectors.toList());

            // 批量查询商品信息
            List<DmShoppingGoods> dmShoppingGoodsList = dmShoppingGoodsMapper.selectGoodsId(goodsIds);

            // 批量查询每个商品的收藏数量并设置到商品对象中
            for (DanmoShoppingProductsCollection shoppingProductsCollection : danmoShoppingProductsCollections) {
                Long goodsId = shoppingProductsCollection.getGoodsId();
                Long collectionCount = danmoShoppingProductsCollectionMapper.selectCollectionCountByGoodsId(goodsId);

                // 将收藏数设置到相应的商品对象中
                for (DmShoppingGoods dmShoppingGoods : dmShoppingGoodsList) {
                    if (dmShoppingGoods.getId().equals(goodsId)) {
                        dmShoppingGoods.setCollectionNumber(collectionCount != null ? collectionCount.intValue() : 0);
                    }
                }
            }

            // 将 DmShoppingGoods 设置到 DanmoShoppingProductsCollection 中
            for (DanmoShoppingProductsCollection shoppingProductsCollection : danmoShoppingProductsCollections) {
                for (DmShoppingGoods dmShoppingGoods : dmShoppingGoodsList) {
                    if (shoppingProductsCollection.getGoodsId().equals(dmShoppingGoods.getId())) {
                        shoppingProductsCollection.setDmShoppingGoods(dmShoppingGoods);
                    }
                }
            }

            return danmoShoppingProductsCollections;
        }




    @Override
    public Result<Integer> deleteDanmoById(DanmoShoppingProductsCollection danmoShoppingProductsCollection) {

//        DanmoShoppingProductsCollection danmoShoppingProductsCollection1 = danmoShoppingProductsCollectionMapper.selectDanmoShoppingProductsCollectionLists(danmoShoppingProductsCollection);
        int i = danmoShoppingProductsCollectionMapper.deleteDanmoShoppingProducts(danmoShoppingProductsCollection);
        return Result.success(i,"取消成功");
    }

    /**
     * 新增商品信息
     *
     * @param danmoShoppingProductsCollection 商品收藏对象
     */
    public void insertDanmoShoppingProducts(DanmoShoppingProductsCollection danmoShoppingProductsCollection)
    {
        List<DanmoShoppingProducts> danmoShoppingProductsList = danmoShoppingProductsCollection.getDanmoShoppingProductsList();
        Long id = danmoShoppingProductsCollection.getId();
        if (StringUtils.isNotNull(danmoShoppingProductsList))
        {
            List<DanmoShoppingProducts> list = new ArrayList<DanmoShoppingProducts>();
            for (DanmoShoppingProducts danmoShoppingProducts : danmoShoppingProductsList)
            {
                danmoShoppingProducts.setId(id);
                list.add(danmoShoppingProducts);
            }
            if (list.size() > 0)
            {
                danmoShoppingProductsCollectionMapper.batchDanmoShoppingProducts(list);
            }
        }
    }
}
